#!/usr/bin/env python 2.6
#=============================================================================
'''
    CS Module tests using simplesurvey
    This isolates the code in the CS modules from much of the Surveyor
    framework code -- in particular the multi-processing, IO, and folder
    processing.
    Some framework components that modules depend on are used.
'''
#=============================================================================
# Copyright 2011, Matt Peloquin. This file is part of Code Surveyor,
# covered under GNU GPL v3 and is distributed WITHOUT ANY WARRANTY
#=============================================================================
import unittest
import sys
import os

import test_syspathfix
import simplesurvey
from framework import configentry
from framework import trace

TraceLevel = 1
TraceModes = [trace.MODE_FILE, trace.MODE_CONFIG, trace.MODE_NBNC, trace.MODE_NOT_CODE, trace.MODE_SEARCH]

def suite(testNamePrefix='test_', extraArg1=None, extraArg2=None):
    global TraceLevel, TraceModes
    if extraArg1 is not None:
        TraceLevel = extraArg1
    if extraArg2 is not None:
        TraceModes = eval(extraArg2)
    return unittest.makeSuite(CSModuleTests, testNamePrefix)


class CSModuleTests( unittest.TestCase ):

    MetaDataOptions = {
        'NAME': None,
        'DIRS': 4,
        'TAGS': None,
        'DATE': ['%Y-%m-%d'],
        'SIZE': None,
        'MODULE': None,
        }

    @classmethod
    def setUpClass(cls):
        print "================================================="
        print "            Starting csmodule Tests              "
        print "================================================="

    # Path for the output files
    outPath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'output'))

    def setUp(self):
        # Path for each test job's output
        head, tail = os.path.splitext(self.id())
        self.outFile = open(os.path.join(self.outPath, 'CsModule_'+tail[1:]+'.txt'), 'w')

    # Default call to simplesurveyor
    def run_survey(self, testEntries):
        simplesurvey.simple_survey(
                os.path.join('.', 'TestFiles', 'MeasureFiles'),
                testEntries, self.outFile, TraceLevel, TraceModes)

    # Create a configEntry object with appropdiate defaults
    def get_config(self, configStr):
        configEntry = configentry.ConfigEntry(configStr)
        configEntry.options.append(('METADATA', self.MetaDataOptions))
        return configEntry

    #--------------------------------------------------------------------------

    def test_Code(self):
        testEntries = [
            (self.get_config('measure Code  file.*;nbnc.*  EX:*.PDF'), []),
            (self.get_config('search Code  file.*;search.*  *.sql'), ['test']),
            (self.get_config('routines Code  file.*;routine.*  *.java;*.cs;*.c;*.py'), ['test']),
            ]
        self.run_survey(testEntries)

    def test_Document(self):
        testEntries = [
            (self.get_config('measure Document file.*;doc.* *.pdf;*.txt'), ['test']),
            ]
        self.run_survey(testEntries)

    def test_Dupe(self):
        testEntries = [
            (self.get_config('analyze DupeLines  file.*;nbnc.*  *.py'), []),
            ]
        self.run_survey(testEntries)

    def test_Depends(self):
        testEntries = [
            (self.get_config('analyze Depends  file.*;nbnc.*  *.java'), []),
            ]
        self.run_survey(testEntries)

    def test_NBNC(self):
        testEntries = [
            (self.get_config('measure NBNC  file.*;nbnc.*  *'), []),
            ]
        self.run_survey(testEntries)

    def test_Other(self):
        testEntries = [
            (self.get_config('measure customCobol  file.*;nbnc.*  *.cbl'), []),
            (self.get_config('measure customDelphi  file.*;nbnc.* *.pas'), []),
            (self.get_config('measure customProlog  file.*;nbnc.* *.pl'), []),
            (self.get_config('measure customPowerBuilder  file.*;nbnc.*  *.pbl'), []),
            ]
        self.run_survey(testEntries)

    def test_Search(self):
        testEntries = [
            (self.get_config('search Search  file.*;search.*  *'), ['test']),
            (self.get_config('search_multi Search file.*;search.* *.txt'), ['test*.\n*.test']),
            ]
        self.run_survey(testEntries)

    def test_Web(self):
        testEntries = [
            (self.get_config('measure Web  file.*;nbnc.*  *.jsp;*.ascx;*.htm*'), []),
            (self.get_config('search Web  file.*;nbnc.*  *.jsp;*.ascx;*.htm*'), ['test']),
            ]
        self.run_survey(testEntries)

    #--------------------------------------------------------------------------

    def tearDown(self):
        print "Output is in: {0}".format(self.outFile.name)
        self.outFile.close()


if __name__ == '__main__':

    testSuite = suite()
    if len(sys.argv) > 1 and len(sys.argv) <= 4:
        testSuite = suite(*tuple(arg for arg in sys.argv[1:]))

    unittest.TextTestRunner(verbosity=4).run(unittest.TestSuite(testSuite))

